# QtLocation plugin
if(MLN_QT_STATIC)
    qt_add_plugin(
        ${MLN_QT_GEOSERVICES_PLUGIN}
        STATIC
        CLASS_NAME QGeoServiceProviderFactoryMapLibre
        OUTPUT_TARGETS GeoServicesPluginOutputTargets
        PLUGIN_TYPE geoservices
    )
else()
    qt_add_plugin(
        ${MLN_QT_GEOSERVICES_PLUGIN}
        CLASS_NAME QGeoServiceProviderFactoryMapLibre
        OUTPUT_TARGETS GeoServicesPluginOutputTargets
        PLUGIN_TYPE geoservices
    )
endif()

target_sources(
    ${MLN_QT_GEOSERVICES_PLUGIN}
    PRIVATE
        qgeoserviceproviderplugin.cpp qgeoserviceproviderplugin.hpp

        $<$<PLATFORM_ID:Windows>:${CMAKE_BINARY_DIR}/version_info.rc>
)

# QtLocation plugin extra target properties
set_target_properties(
    ${MLN_QT_GEOSERVICES_PLUGIN}
    PROPERTIES
        AUTOMOC ON
        EXPORT_NAME PluginGeoServices
        LIBRARY_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/geoservices>
        RUNTIME_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/geoservices>
)

# QtLocation plugin include directories
target_include_directories(
    ${MLN_QT_GEOSERVICES_PLUGIN}
    PRIVATE
        ${CMAKE_SOURCE_DIR}/src/core
        ${CMAKE_SOURCE_DIR}/src/core/style
        ${CMAKE_BINARY_DIR}/src/core/include
        $<$<BOOL:${MLN_WITH_VULKAN}>:${MLN_CORE_PATH}/vendor/Vulkan-Headers/include>
        $<$<BOOL:${MLN_WITH_VULKAN}>:${MLN_CORE_PATH}/vendor/VulkanMemoryAllocator/include>
)

# QtLocation plugin linked libraries
target_link_libraries(
    ${MLN_QT_GEOSERVICES_PLUGIN}
    PRIVATE
        MLNQtLocation
        Qt${QT_VERSION_MAJOR}::Location
        Qt${QT_VERSION_MAJOR}::LocationPrivate
        $<BUILD_INTERFACE:mbgl-compiler-options>
        $<BUILD_INTERFACE:MLNQtCompilerOptions>
)

# QtLocation plugin development specifics
if(MLN_QT_WITH_CLANG_TIDY)
    set_target_properties(${MLN_QT_GEOSERVICES_PLUGIN} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
endif()

# QtLocation plugin installation
install(
    EXPORT ${MLN_QT_NAME}LocationPluginGeoServicesTargets
    NAMESPACE ${MLN_QT_NAMESPACE}
    DESTINATION ${CMAKECONFIG_INSTALL_DIR}
)
install(
    TARGETS ${MLN_QT_GEOSERVICES_PLUGIN} ${GeoServicesPluginOutputTargets}
    EXPORT ${MLN_QT_NAME}LocationPluginGeoServicesTargets
    ARCHIVE DESTINATION "plugins/geoservices"
    LIBRARY DESTINATION "plugins/geoservices"
    OBJECTS DESTINATION "plugins/geoservices"
    RUNTIME DESTINATION "plugins/geoservices"
)

# QtLocation QML extension plugin
set(Plugin_Sources
    qml_types.hpp
    declarative_style.cpp declarative_style.hpp
    declarative_style_parameter.hpp
    declarative_filter_parameter.cpp declarative_filter_parameter.hpp
    declarative_image_parameter.cpp declarative_image_parameter.hpp
    declarative_layer_parameter.cpp declarative_layer_parameter.hpp
    declarative_source_parameter.cpp declarative_source_parameter.hpp

    $<$<PLATFORM_ID:Windows>:${CMAKE_BINARY_DIR}/version_info.rc>
)

if(MLN_QT_STATIC)
    qt_add_qml_module(
        ${MLN_QT_QML_PLUGIN_LOCATION}
        STATIC
        URI MapLibre.Location
        VERSION ${PROJECT_VERSION}
        PLUGIN_TARGET ${MLN_QT_QML_PLUGIN_LOCATION}
        NO_PLUGIN_OPTIONAL
        NO_GENERATE_QMLDIR
        CLASS_NAME MapLibreQmlLocationModule
        RESOURCE_PREFIX "/"
        OUTPUT_DIRECTORY "MapLibre/Location"
        OUTPUT_TARGETS QmlLocationPluginOutputTargets
        SOURCES ${Plugin_Sources}
    )
else()
    qt_add_qml_module(
        ${MLN_QT_QML_PLUGIN_LOCATION}
        URI MapLibre.Location
        VERSION ${PROJECT_VERSION}
        PLUGIN_TARGET ${MLN_QT_QML_PLUGIN_LOCATION}
        NO_PLUGIN_OPTIONAL
        NO_GENERATE_QMLDIR
        CLASS_NAME MapLibreQmlLocationModule
        RESOURCE_PREFIX "/"
        OUTPUT_DIRECTORY "MapLibre/Location"
        OUTPUT_TARGETS QmlLocationPluginOutputTargets
        SOURCES ${Plugin_Sources}
    )
endif()
set_target_properties(
    ${MLN_QT_QML_PLUGIN_LOCATION}
    PROPERTIES
        LIBRARY_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/MapLibre/Location>
        RUNTIME_OUTPUT_DIRECTORY $<1:${CMAKE_CURRENT_BINARY_DIR}/MapLibre/Location>
)
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/qmldir.in"
    "${CMAKE_CURRENT_BINARY_DIR}/MapLibre/Location/qmldir"
    @ONLY
)
set_property(TARGET ${MLN_QT_QML_PLUGIN_LOCATION} PROPERTY EXPORT_NAME PluginQmlLocation)

# QtLocation QML extension plugin include directories
target_include_directories(
    ${MLN_QT_QML_PLUGIN_LOCATION}
    PRIVATE
        ${CMAKE_SOURCE_DIR}/src/core
        ${CMAKE_SOURCE_DIR}/src/core/style
        ${CMAKE_BINARY_DIR}/src/core/include
)

# QtLocation QML extension plugin linked libraries
target_link_libraries(
    ${MLN_QT_QML_PLUGIN_LOCATION}
    PRIVATE
        MLNQtLocation
        Qt${QT_VERSION_MAJOR}::LocationPrivate
        $<BUILD_INTERFACE:mbgl-compiler-options>
        $<BUILD_INTERFACE:MLNQtCompilerOptions>
)

# QtLocation QML extenstion plugin development specifics
if(MLN_QT_WITH_CLANG_TIDY)
    set_target_properties(${MLN_QT_QML_PLUGIN_LOCATION} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}")
endif()

# QtLocation QML extension plugin installation
install(
    EXPORT ${MLN_QT_NAME}LocationPluginQmlTargets
    NAMESPACE ${MLN_QT_NAMESPACE}
    DESTINATION ${CMAKECONFIG_INSTALL_DIR}
)
install(
    TARGETS ${MLN_QT_QML_PLUGIN_LOCATION} ${QmlLocationPluginOutputTargets}
    EXPORT ${MLN_QT_NAME}LocationPluginQmlTargets
    ARCHIVE DESTINATION "qml/MapLibre/Location"
    LIBRARY DESTINATION "qml/MapLibre/Location"
    OBJECTS DESTINATION "qml/MapLibre/Location"
    RUNTIME DESTINATION "qml/MapLibre/Location"
)
install(
    FILES
        "${CMAKE_CURRENT_BINARY_DIR}/MapLibre/Location/qmldir"
    DESTINATION "qml/MapLibre/Location"
)
install(
    FILES
        "${CMAKE_CURRENT_BINARY_DIR}/MapLibre/Location/${MLN_QT_QML_PLUGIN_LOCATION}.qmltypes"
    DESTINATION "qml/MapLibre/Location"
)
